<?php

namespace App\Service\Common;

use App\Common\ResponseCode;
use App\Exceptions\BusinessException;
use App\Params\Common\AlbumDeleteParams;
use App\Params\Common\AlbumEditParams;
use App\Params\Common\AlbumListParams;
use App\Repository\MerchantsInfoRepo;
use App\Repository\SpecialAlbumRepo;
use App\Repository\SpecialRepo;
use App\Service\BaseService;
use Illuminate\Support\Facades\Log;
use App\Params\Common\AlbumDetailParams;

class AlbumService extends BaseService
{
    public function getList(AlbumListParams $params): array
    {
        $mid = (string)$params->getMid();
        if (empty($mid)) {
            return [
                'total' => 0,
                'list' => []
            ];
        }
        $merchantInfo = MerchantsInfoRepo::getInstance()->getInfoByAccessMid($params->getMid());

        if (empty($merchantInfo)) {
            return ['total' => 0,
                'list' => []];
        }

        $condition = $params->toArray();
        $condition['merchants_id'] = $merchantInfo['merchants_id'];

        $query = SpecialAlbumRepo::getInstance()->getQuery($condition);
        $data = $this->toReturnList($query, $params->getPage(), $params->getPageSize());

        foreach ($data['list'] as $item) {
            $item->count = SpecialRepo::getInstance()->getCountByCondition(['special_album_id' => $item->id]);
        }
        return $data;
    }

    /**
     * @param AlbumDeleteParams $params
     * @return bool|mixed|null
     * @throws BusinessException
     */
    public function delete(AlbumDeleteParams $params)
    {
        $merchantInfo = MerchantsInfoRepo::getInstance()->getInfoByAccessMid($params->getMid());
        Log::info("[专辑]获取接入商户信息".json_encode($merchantInfo,JSON_UNESCAPED_UNICODE));
        if (empty($merchantInfo)) {
            throw new BusinessException(ResponseCode::MERCHANTS_NOT_FUND);
        }

        $album = SpecialAlbumRepo::getInstance()->getAlbumById($params->getId());
        Log::info("[专辑]获取专辑信息".json_encode($album,JSON_UNESCAPED_UNICODE));
        if (empty($album)) {
            throw new BusinessException(ResponseCode::SPECIAL_ALBUM_NOT_FOUND);
        }

        if ($album['merchants_id'] != $merchantInfo['merchants_id'])
            throw new BusinessException(ResponseCode::SPECIAL_ALBUM_NOT_FOUND);

        $exist = SpecialRepo::getInstance()->isExist(['special_album_id' => $album->id]);

        if ($exist) {
            throw new BusinessException(ResponseCode::SPECIAL_ALBUM_NOT_ALLOWED);
        }

        return $album->delete();
    }

    /**
     * @param AlbumEditParams $params
     * @return int
     * @throws BusinessException
     */
    public function edit(AlbumEditParams $params): int
    {

        $merchantInfo = MerchantsInfoRepo::getInstance()->getInfoByAccessMid($params->getMid());
        Log::info("[专辑]获取接入商户信息".json_encode($merchantInfo,JSON_UNESCAPED_UNICODE));
        if (empty($merchantInfo)) {
            throw new BusinessException(ResponseCode::MERCHANTS_NOT_FUND);
        }
        $data = [
            'merchants_id' => $merchantInfo['merchants_id'],
            'name' => $params->getName(),
            'desc' => $params->getDesc(),
            'cover' => $params->getCover(),
            'is_resale' => 1,
            'is_donation' => 1,
            'update_time' => date('Y-m-d H:i:s'),
        ];
        if ($params->getId()) {
            $album = SpecialAlbumRepo::getInstance()->getAlbumById($params->getId());
            Log::info("[专辑]获取专辑信息".json_encode($album,JSON_UNESCAPED_UNICODE));
            if (empty($album)) {
                throw new BusinessException(ResponseCode::SPECIAL_ALBUM_NOT_FOUND);
            }

            if ($album['merchants_id'] != $merchantInfo['merchants_id'])
                throw new BusinessException(ResponseCode::SPECIAL_ALBUM_NOT_FOUND);
            unset($data['merchants_id']);
        }

        if (!$params->getId()) {
            $data['sell_money_count'] = '0.00';
        }
        return SpecialAlbumRepo::getInstance()->save($data, $params->getId());
    }

    /**
     * 专辑详情
     * @param AlbumDetailParams $params
     * @return array
     * @throws BusinessException
     * Author:xsc
     * Time:2023/11/22 17:27
     */
    public function detail(AlbumDetailParams $params): array
    {
        $access_mid = $params->getMid();
        $id = $params->getId();

        // 接入商户信息
        $merchants_info = MerchantsInfoRepo::getInstance()->getInfoByAccessMid($access_mid);
        if (!$merchants_info) {
            throw new BusinessException(ResponseCode::MERCHANTS_NOT_FUND);
        }

        $album = SpecialAlbumRepo::getInstance()->getAlbumById($id);
        if (!$album || $album->merchants_id != $merchants_info['merchants_id']) {
            throw new BusinessException(ResponseCode::SPECIAL_ALBUM_NOT_FOUND);
        }

        $data = [
            'id'          => $id,
            'name'        => $album['name'],
            'desc'        => $album['desc'],
            'cover'       => $album['cover'],
            'create_time' => $album['create_time'],
            'count'       => SpecialRepo::getInstance()->getCountByCondition(['special_album_id' => $id]),
        ];

        return $data;
    }

}
